from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
from markdown import markdown, Markdown
# from markdown2 import markdown, Markdown

from Markdown.forms import ArticleForm
from Markdown.models import Article, BlogPost


def publish_article(request):
    if request.method == 'POST':
        title = request.POST.get('title')
        desc = request.POST.get('desc')
        content = request.POST.get('content')
        save_number = request.POST.get('save_number')
        user = request.POST.get('user')

        article = Article.objects.create(title=title, desc=desc, content=content, save_number=save_number,
                                         user_id=user)
        if article:
            return HttpResponse('发表成功')
    article_form = ArticleForm()
    return render(request, 'publish_article.html', locals())


def show_article(request):
    article = Article.objects.get(pk=3)
    article.content = markdown(article.content,
                               extensions=['markdown.extensions.extra', 'markdown.extensions.codehilite', ])

    return render(request, 'show_article.html', locals())


def publish_post(request):
    if request.method == 'POST':
        title = request.POST.get('title')
        number = request.POST.get('save_number')
        content = request.POST.get('mytextarea')
        post = BlogPost.objects.create(title=title,save_number=number,body=content,user_id=4)
        if post:
            return HttpResponse('ok')
    return render(request, 'post.html')